const { program } = require("commander");
const Keys = require("../utils/keys");

program
  .description("manage tinypng keys")
  .option("-a, --add <items>", "Add keys")
  .option("-r, --remove <items>", "Remove keys")
  .option("-c, --clear", "Clear keys")
  .option("-l, --list", "Check keys")
  .helpOption("-h, --help", "Output usage information")
  .action(action);

function action(cmd) {
  // 添加keys
  if (cmd.add) {
    Keys.addKeys(cmd.add);
  }

  // 移除keys
  if (cmd.remove) {
    Keys.removeKeys(cmd.remove);
  }

  // 清空keys
  if (cmd.clear) {
    Keys.clearKeys();
  }

  // 查看keys
  if (cmd.list) {
    const keys = Keys.getKeys();
    console.log(keys.join("\n"));
  }
}

if (!process.argv.slice(2).length) {
  program.outputHelp();
}

program.parse(process.argv);
